/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.videodes.desktop;

import cz.muni.fi.pb138.videodes.entity.Dvd;
import cz.muni.fi.pb138.videodes.entity.DvdType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author vaclav
 */
public class DvdTableModel extends AbstractTableModel {

    private List<Dvd> dvds;
    
    public DvdTableModel() {
        dvds = new ArrayList<Dvd>();
    }
    
    public DvdTableModel(List<Dvd> dvds) {
        this.dvds = dvds;
        
        int lastRow = this.dvds.size() - 1;
        fireTableRowsInserted(lastRow, lastRow);
    }
    
    public void add(Dvd dvd) {
        dvds.add(dvd);
        
        int lastRow = dvds.size() - 1;
        fireTableRowsInserted(lastRow, lastRow);
    }
    
    public void remove(Dvd dvd) {
        dvds.remove(dvd);
        
        int lastRow = dvds.size() - 1;
        fireTableRowsDeleted(lastRow, lastRow);
    }
    
    public void update(Dvd dvd) {
        for (Dvd d : dvds) {
            if (d.equals(dvd)) {
                d.setTitle(dvd.getTitle());
                d.setType(dvd.getType());
                d.setContent(dvd.getContent());
                
                fireTableRowsUpdated(dvds.size()-1, dvds.size()-1);
                return;
            }
        }
    }
    
    public List<Dvd> getAllDvds() {
        return Collections.unmodifiableList(dvds);
    }

    @Override
    public int getRowCount() {
        return dvds.size();
    }

    @Override
    public int getColumnCount() {
        return 3;
    }

    @Override
    public Object getValueAt(int r, int c) {
        Dvd dvd = dvds.get(r);
        switch (c) {
            case 0:
                return dvd.getTitle();
            case 1:
                return dvd.getType();
            case 2:
                return dvd.getContent().size();
            case -1:
                return dvd.getId();
            case -2:
                return dvd.getContent();
            case -3:
                return dvd;
            default:
                throw new IllegalArgumentException("Invalid column index: " + c);
        }
    }
    
    @Override
    public String getColumnName(int c) {
        switch (c) {
            case 0:
                return "Title";
            case 1:
                return "Type";
            case 2:
                return "Numer of movies";
            default:
                throw new IllegalArgumentException("Invalid column index: " + c);
        }
    }
    
    @Override
    public Class<?> getColumnClass(int c) {
        switch (c) {
            case 0:
                return String.class;
            case 1:
                return DvdType.class;
            case 2:
                return Integer.class;
            default:
                throw new IllegalArgumentException("Invalid column index: " + c);
        }
    }
}
